//
//  OCSSNetRequest.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/10/16.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit
import Alamofire
import PKHUD

enum MethodType {
    case get
    case post
}

typealias successBlock = (_ result: Any)->();
typealias failureBlock = (_ error: Any)->();
typealias resultBlock = (_ result: Any)->();

class OCSSNetRequest: NSObject {
    
    var isLogin = false;
    
    class func requestNetData(url:String, para:[String:Any], type: MethodType, isLogin:Bool=false, result: @escaping resultBlock) -> Void{
        let method = type == .get ? HTTPMethod.get : HTTPMethod.post;
        let urlStr = BaseRequestUrl+url;
        let cookie = UserDefaults.standard.object(forKey: "Cookie");
        var headers:HTTPHeaders?;
        if (cookie != nil) {
            headers = ["Cookie":cookie as! String];
        }
        Alamofire.request(urlStr, method: method, parameters: para, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
            if isLogin {
                if (response.response?.isKind(of: HTTPURLResponse.classForCoder()))!{
                    let res = response.response;
                    let cookie = res?.allHeaderFields["Set-Cookie"];
                    let arr = (cookie as! NSString).components(separatedBy: ";")
                    UserDefaults.standard.set(arr[0], forKey: "Cookie");
                    UserDefaults.standard.synchronize();
                }
            }
            result(response.result);
        }
    }
    
    class func requestData(url:String ,para:[String:Any], type: MethodType ,isLogin:Bool=false, success: @escaping successBlock, failure: @escaping failureBlock) -> Void {
        HUD.show(.progress);
        let method = type == .get ? HTTPMethod.get : HTTPMethod.post;
        let urlStr = BaseRequestUrl+url;
        print("url:"+urlStr);
        print("parameters:\(para)");
        let cookie = UserDefaults.standard.object(forKey: "Cookie");
        var headers:HTTPHeaders?;
        if (cookie != nil) {
            headers = ["Cookie":cookie as! String];
        }
        
        Alamofire.request(urlStr, method: method, parameters: para, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
            HUD.hide();
            guard let result = response.result.value else {
                failure(response.result.error!);
                HUD.flash(HUDContentType.error);
                return;
            }
            
            print("result:\(result)");
            if isLogin {
                if (response.response?.isKind(of: HTTPURLResponse.classForCoder()))!{
                    let res = response.response;
                    let cookie = res?.allHeaderFields["Set-Cookie"];
                    let arr = (cookie as! NSString).components(separatedBy: ";")
                    UserDefaults.standard.set(arr[0], forKey: "Cookie");
                    UserDefaults.standard.synchronize();
                    
                }
            }
            success(result);
            HUD.flash(.success);
        };
    }
}
